package com.ecoolex.chinadutyfree.retailer.api.graphql;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;

import com.ecoolex.chinadutyfree.retailer.entity.GoodsAfterService;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsBarcode;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsBrand;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsCategory;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsProduct;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsSpec;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsSpecGroup;
import com.ecoolex.chinadutyfree.retailer.entity.GoodsWithBLOBs;
import com.ecoolex.chinadutyfree.retailer.ws.GoodsService;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyType;
import com.ecoolex.framework.modularize.api.service.currency.CurrencyTypeService;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;

@GraphQL2Module(type=Type.User,description = "商品查询")
public class GoodsGraphql {

	@Reference
	private GoodsService service;
	
	@Autowired
	private StorageService storageService;

	@Autowired
	private CurrencyTypeService currencyTypeService;
	
	@TypeField(type = GoodsWithBLOBs.class, field = "brand", description = "商品品牌")
	public GoodsBrand brand(@Source GoodsWithBLOBs source) {
		return service.getBrandById(source.getBrandId());
	}

	@TypeField(type = GoodsWithBLOBs.class, field = "category", description = "商品分类")
	public GoodsCategory category(@Source GoodsWithBLOBs source) {
		return service.getCategoryById(source.getCategoryId());
	}

	@TypeField(type = GoodsWithBLOBs.class, field = "currencyType", description = "币种")
	public CurrencyType currencyType(@Source GoodsWithBLOBs source) {
		return currencyTypeService.get(source.getCurrencyType());
	}

	@TypeField(type = GoodsWithBLOBs.class, field = "image", description = "商品展示图")
	public StorageInfo image(@Source GoodsWithBLOBs source) {
		return storageService.get(source.getImage());
	}

	@TypeField(type = GoodsWithBLOBs.class, field = "afterService", description = "售后模板")
	public GoodsAfterService afterService(@Source GoodsWithBLOBs source) {
		return service.getAfterServiceById(source.getAfterServiceId());
	}

	@TypeField(type = GoodsWithBLOBs.class, field = "shopwindowList", description = "商品橱窗图列表")
	public List<StorageInfo> shopwindowList(@Source GoodsWithBLOBs source) {
		return service.queryGoodsShopwindowByGoodsId(source.getGoodsId()).stream().map(
			shopwindow -> storageService.get(shopwindow.getImage())).collect(Collectors.toList());
	}

	@TypeField(type = GoodsWithBLOBs.class, field = "specGroupList", description = "规格分组列表")
	public List<GoodsSpecGroup> specGroupList(@Source GoodsWithBLOBs source) {
		return service.queryGoodsSpecGroupByGoodsId(source.getGoodsId());
	}

	@TypeField(type = GoodsSpecGroup.class, field = "specList", description = "规格值列表")
	public List<GoodsSpec> specList(@Source GoodsSpecGroup source) {
		return service.queryGoodsSpecByGroupId(source.getSpecGroupId());
	}

	@TypeField(type = GoodsWithBLOBs.class, field = "productList", description = "商品产品列表")
	public List<GoodsProduct> productList(@Source GoodsWithBLOBs source) {
		return service.queryGoodsProductByGoodsId(source.getGoodsId());
	}

	@TypeField(type = GoodsWithBLOBs.class, field = "totalSoldNumber", description = "总计销售数量")
	public Long sumSoldNumber(@Source GoodsWithBLOBs source) {
		return service.sumSoldNumberByGoodsId(source.getGoodsId());
	}

	@TypeField(type = GoodsProduct.class, field = "image", description = "产品展示图")
	public StorageInfo image(@Source GoodsProduct source) {
		return storageService.get(source.getImage());
	}

	@TypeField(type = GoodsProduct.class, field = "barcodeList", description = "产品条码列表")
	public List<String> productList(@Source GoodsProduct source) {
		return service.queryGoodsBarcodeByProductId(source.getProductId()).stream().map(
			GoodsBarcode::getBarCode).collect(Collectors.toList());
	}
}
